<template>
  <div class="business">
    <div class="title">
      <span>出差申请</span>
      <i class="el-icon-star-on"></i>
      <i class="el-icon-scissors"></i>
      <i class="el-icon-more"></i>
    </div>
    <div class="sub-title">
      <el-button @click="drawer = true" type="primary" class="button add">
        发起新的
      </el-button>
      <el-upload
        class="upload"
        action="/api/business/import"
        :headers="headers"
        :data="{ userid: userinfo._id }"
        name="excel"
        :show-file-list="false"
        :on-success="onSuccess"
      >
        <el-button class="button putin" size="small">导入</el-button>
      </el-upload>
      <el-tabs v-model="activeName">
        <el-tab-pane label="待办事项" name="1">
          <MyTable
            :tableData="tableData"
            :handleCurrentChange="handleCurrentChange"
            :deleteRow="deleteRow"
            :total="total"
            :clo="clo"
          ></MyTable>
        </el-tab-pane>
        <el-tab-pane label="已办事项" name="2"
          ><MyTable
            :tableData="tableData"
            :handleCurrentChange="handleCurrentChange"
            :deleteRow="deleteRow"
            :total="total"
            :clo="clo"
          ></MyTable
        ></el-tab-pane>
        <el-tab-pane label="抄送事项" name="3"
          ><MyTable
            :tableData="tableData"
            :handleCurrentChange="handleCurrentChange"
            :deleteRow="deleteRow"
            :total="total"
            :clo="clo"
          ></MyTable
        ></el-tab-pane>
        <el-tab-pane label="我发起的" name="4"
          ><MyTable
            :tableData="tableData"
            :handleCurrentChange="handleCurrentChange"
            :deleteRow="deleteRow"
            :total="total"
            :clo="clo"
          ></MyTable
        ></el-tab-pane>
        <el-tab-pane label="全部数据" name="5"
          ><MyTable
            :tableData="tableData"
            :handleCurrentChange="handleCurrentChange"
            :deleteRow="deleteRow"
            :total="total"
            :clo="clo"
          ></MyTable
        ></el-tab-pane>
        <el-tab-pane label="概览" name="6"
          ><MyTable
            :tableData="tableData"
            :handleCurrentChange="handleCurrentChange"
            :deleteRow="deleteRow"
            :total="total"
            :clo="clo"
          ></MyTable
        ></el-tab-pane>
        <el-tab-pane label="统计报表" name="7">
          <MyEcharts></MyEcharts>
        </el-tab-pane>
      </el-tabs>
    </div>

    <!-- 抽屉 -->
    <el-drawer title="出差申请" :visible.sync="drawer" modal size="50%">
      <div class="shadow"></div>
      <div class="form-title">出差申请</div>
      <!-- 主要区域 -->
      <div class="form-main">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          class="form-box"
        >
          <p>个人信息</p>
          <el-form-item label="申请人" prop="name">
            <el-input
              type="text"
              v-model="ruleForm.name"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="出差事由" prop="reason">
            <el-input
              type="textarea"
              placeholder="填写出差事由"
              v-model="ruleForm.reason"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <div class="flex">
            <el-form-item label="开始时间" prop="starttime">
              <el-date-picker
                @blur="this.getday"
                v-model="ruleForm.starttime"
                type="date"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间" prop="endtime">
              <el-date-picker
                @blur="this.getday"
                v-model="ruleForm.endtime"
                type="date"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
          </div>
          <el-form-item label="出差天数" prop="days">
            <el-input
              type="num"
              v-model="ruleForm.days"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <!-- 底部区域 -->
      <div class="form-bottom">
        <el-button class="buttton" type="primary" @click="submitForm"
          >提交</el-button
        >
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { business_list, business_add, business_delete } from "../../utils/api";
import { mapState } from "vuex";
import MyTable from "../../components/MyTable.vue";
import MyEcharts from "../../components/MyEcharts.vue";
export default {
  data() {
    var validateName = (rule, value, callback) => {
      if (value.length < 2) {
        callback(new Error("请输入正确的姓名"));
      } else {
        callback();
      }
    };
    var validateReason = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请输入出差事由"));
      } else {
        callback();
      }
    };
    var validateValue1 = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请选择开始日期"));
      } else {
        callback();
      }
    };
    var validateValue2 = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请选择结束日期"));
      } else {
        callback();
      }
    };
    var validateDay = (rule, value, callback) => {
      if (value == "") {
        callback(new Error("请选择开始和结束日期"));
      } else {
        callback();
      }
    };
    return {
      activeName: "1", //tab表格切换
      headers: { Authorization: "Bearer " + localStorage.getItem("token") }, //请求头
      multipleSelection: [], //全选相关
      page: 1,
      total: 0,
      drawer: false,
      activeIndex: 1,
      tableData: [],
      ruleForm: {
        userid: this.$store.state.userinfo._id,
        name: this.$store.state.userinfo.name,
        reason: "",
        starttime: "",
        endtime: "",
        days: "",
      },
      rules: {
        name: [{ validator: validateName, trigger: "blur" }],
        reason: [{ validator: validateReason, trigger: "blur" }],
        starttime: [{ validator: validateValue1, trigger: "blur" }],
        endtime: [{ validator: validateValue2, trigger: "blur" }],
        days: [{ validator: validateDay, trigger: "blur" }],
      },
      clo: [
        {
          prop: "_id",
          label: "编号",
        },
        {
          prop: "status",
          label: "当前流程状态",
          formater: this.formater_status,
        },
        {
          prop: "name",
          label: "申请人",
        },
        {
          prop: "reason",
          label: "出差事由",
        },
        {
          prop: "starttime",
          label: "出发日期",
          formater: this.formatterTime,
        },
        {
          prop: "endtime",
          label: "返回日期",
          formater: this.formatterTime,
        },
        {
          prop: "days",
          label: "出差总天数",
        },
      ],
    };
  },
  components: {
    MyTable,
    MyEcharts,
  },
  mounted() {
    business_list({ userid: this.userinfo._id, page: 1 }).then((res) => {
      this.tableData = res.data.list;
      this.total = res.data.total;
    });
  },
  computed: {
    ...mapState(["userinfo"]),
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          business_add(this.ruleForm).then((res) => {
            if (res.data.code == 200) {
              this.$message({ message: res.data.msg, type: "success" });
              this.tableData = res.data.list;
              this.total = res.data.total;
            } else {
              this.$message({ message: res.data.msg, type: "error" });
            }
          });
        } else {
          this.$message({ message: "请按要求填写信息", type: "error" });
        }
      });
    },
    getday() {
      if (this.ruleForm.starttime != "" && this.ruleForm.endtime != "") {
        let start = new Date(this.ruleForm.starttime);
        let end = new Date(this.ruleForm.endtime);
        let time = (end - start) / 1000 / 60 / 60 / 24;
        if (time >= 0) {
          this.ruleForm.days = time + 1;
        } else {
          this.ruleForm.days = "";
        }
      }
    },
    deleteRow(id) {
      //删除对应id的 出差申请
      business_delete({ userid: this.userinfo._id, id, page: this.page }).then(
        (res) => {
          this.tableData = res.data.list;
          this.total = res.data.total;
        }
      );
    },
    handleCurrentChange(val) {
      this.page = val; //保存页码
      //请求对应页码的数据
      business_list({ userid: this.userinfo._id, page: val }).then((res) => {
        this.tableData = res.data.list;
      });
    },
    onSuccess(res) {
      if (res.code == 200) {
        this.$message.success(res.msg);
        this.tableData.push(...res.list);
        this.tableData = this.tableData.slice(0, 10);
        this.total += res.total;
      } else {
        this.$message.error(res.msg);
      }
    },
    formater_status(row, column, cellValue, index) {
      return cellValue == 0 ? "待审批" : "已通过";
    },
    formater_num(num) {
      return num < 10 ? "0" + num : num;
    },
    formatterTime(row, column, cellValue, index) {
      let day = new Date(cellValue);
      return `${day.getFullYear()}-${this.formater_num(
        day.getMonth() + 1
      )}-${this.formater_num(day.getDate())}`;
    },
  },
};
</script>

<style lang='scss'>
.business {
  .title {
    line-height: 79px;
    padding-left: 20px;
    font-weight: 600;
    display: flex;
    align-items: center;
    border: 1px solid #eee;
    span {
      color: #333;
      margin-right: 10px;
    }
    .el-icon-star-on {
      font-size: 20px;
      color: orange;
      margin-right: 10px;
    }
    .el-icon-scissors {
      color: #ccc;
      margin-right: 10px;
    }
    .el-icon-more {
      color: #ccc;
      margin-right: 30px;
    }
  }
  .sub-title {
    display: flex;
    align-items: center;
    position: relative;
    .button {
      line-height: normal;
      padding: 9px 20px;
      border: 1px solid #ccc;
      border-radius: 5px;
      font-size: 14px;
      cursor: pointer;
      margin: 0 10px;
      background-color: orange;
      color: white;
      position: absolute;
    }
    .add {
      top: 8px;
      left: 8px;
    }
    .putin {
      top: 8px;
      left: 120px;
    }
    .el-tabs {
      width: 100%;
      .el-tabs__header {
        position: absolute;
        top: 8px;
        left: 240px;
        .el-tabs__nav-wrap::after {
          background-color: transparent;
        }
      }
      .el-tabs__content {
        margin-top: 54px;
        width: 100%;
        border-top: 1px solid #e6e6e6;
      }
      .el-tabs__active-bar {
        background-color: orange;
      }
      .el-tabs__item {
        color: #333;
      }
      .is-active {
        color: #333;
      }
    }
  }

  // 弹出层
  .el-drawer__container {
    background-color: rgba($color: #000000, $alpha: 0.4);
  }
  .el-drawer__header {
    span {
      font-weight: 600;
    }
  }
  .el-drawer__body {
    background-color: #f9fafc;
    position: relative;
    display: flex;
    flex-direction: column;
    .shadow {
      top: 0;
      left: 0;
      width: 100%;
      height: 0;
      position: absolute;
      box-shadow: 0 0 5px 2px #e6e6e6;
    }
    .form-title {
      height: 10%;
      width: 90%;
      margin: auto;
      font-size: 20px;
      font-weight: 600;
      background: #fff;
      box-shadow: 0 0 5px 2px #e6e6e6;
      display: flex;
      align-items: center;
      padding-left: 15px;
      border-radius: 0 0 10px 10px;
      margin-bottom: 10px;
    }
    .form-main {
      flex: 1;
      width: 90%;
      margin: 0 auto;
      background: #fff;
      box-shadow: 0 0 5px 2px #e6e6e6;
      border-radius: 10px;
      padding: 15px;
      font-size: 14px;
      p {
        margin-bottom: 20px;
        font-weight: 600;
        color: #333;
      }
      .form-box {
        .flex {
          display: flex;
          justify-content: space-between;
          .el-date-editor.el-input {
            width: 300px;
          }
        }
      }
    }
    .form-bottom {
      height: 10%;
      width: 90%;
      margin: auto;
      background: #fff;
      box-shadow: 0 0 5px 2px #e6e6e6;
      margin-top: 10px;
      border-radius: 10px 10px 0 0;
      display: flex;
      justify-content: center;
      align-items: center;
      button {
        background-color: orange;
        border: 0;
      }
    }
  }
  #main{
  width: 500px;
  height: 300px;
}
}
</style>